﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Collections;
using Liang.DataBase.Helper;

namespace Liang.UI
{

    /// <summary>
    /// 申明委托
    /// </summary>
    /// <param name="e"></param>
    /// <returns></returns>
    public delegate int EventPagingHandler();

    public delegate EventPagingArg EventBtnHandler();


    /// <summary>
    /// 分页控件呈现
    /// </summary>
    public partial class Pager : UserControl
    {
        public Pager()
        {
            InitializeComponent();
            cmbPageSize.SelectedIndex = 1;
        }


        public event EventPagingHandler EventPaging;

        public event EventBtnHandler EventExcel;

        public event EventBtnHandler EventDelItem;
      
        /// <summary>
        /// 每页显示记录数
        /// </summary>
        private int _pageSize = 0;
    
        /// <summary>
        /// 每页显示记录数
        /// </summary>
        public int PageSize
        {
            get { return _pageSize; }
            set
            {
                _pageSize = value;
                GetPageCount();
            }
        }

        private int _nMax = 0;
       
        /// <summary>
        /// 总记录数
        /// </summary>
        public int NMax
        {
            get { return _nMax; }
            set
            {
                _nMax = value;
                GetPageCount();
            }
        }

        private int _pageCount = 0;
       
        /// <summary>
        /// 页数=总记录数/每页显示记录数
        /// </summary>
        public int PageCount
        {
            get { return _pageCount; }
            set { _pageCount = value; }
        }

        private int _pageCurrent = 0;
   
        /// <summary>
        /// 当前页号
        /// </summary>
        public int PageCurrent
        {
            get { return _pageCurrent; }
            set { _pageCurrent = value; }
        }


        private EventPagingArg _epa;

        /// <summary>
        /// 数据类
        /// </summary>
        public EventPagingArg Epa
        {
            get { return _epa; }
            set { _epa = value; }
        }

        private DataGridView _dataView;

        public DataGridView DataView
        {
            get { return _dataView; }
            set { _dataView = value; }
        }

        private string _title;

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }


        /// <summary>
        /// 设置页面大小
        /// </summary>
        private void GetPageCount()
        {
            if (this.NMax > 0)
            {
                this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.NMax) / Convert.ToDouble(this.PageSize)));
                lblPageCount.Text = " / " + PageCount.ToString();
                lblPageCount1.Text = "每页 " + PageSize.ToString() + " 条，共 " + PageCount.ToString() + " 页";
            }
            else
            {
                this.PageCount = 0;
            }
        }

      
        /// <summary>
        /// 翻页控件数据绑定
        /// </summary>
        public void Bind()
        {
            if (null == this.EventPaging)
            {
                return;
            }

            this.NMax = this.EventPaging();

            //if (this.PageCount == 0)
            //{
            //    this.PageCurrent = 0;
            //}

            //if (this.PageCurrent >= this.PageCount)
            //{
            //    this.PageCurrent = this.PageCount;
            //}
            //if (this.PageCount == 1)
            //{
            //    this.PageCurrent = 0;
            //}
            lblcurentpage.Text = string.Format("{0}", (PageCurrent + 1));
            lblRecordCount.Text = "共有 " + NMax.ToString() + " 条记录";
            btnPrev.Enabled = true;
            btnFirst.Enabled = true;
            btnLast.Enabled = true;
            btnNext.Enabled = true;

            if (this.PageCurrent == 0)
            {
                this.btnPrev.Enabled = false;
                this.btnFirst.Enabled = false;
            }
            if (1 == this.PageCount)
            {
                this.btnLast.Enabled = false;
                this.btnNext.Enabled = false;
            }
            if (this.NMax == 0)
            {
                btnNext.Enabled = false;
                btnLast.Enabled = false;
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
            }
            cmbPagecount.Items.Clear();
            for (int i = 1; i <= PageCount; i++)
                cmbPagecount.Items.Add(i.ToString());
            cmbPagecount.SelectedIndex = PageCurrent;
        }
       

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFirst_Click(object sender, EventArgs e)
        {
            PageCurrent = 0;
            this.Bind();
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrev_Click(object sender, EventArgs e)
        {
            PageCurrent -= 1;
            if (PageCurrent < 0)
            {
                PageCurrent = 0;
            }
            this.Bind();
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNext_Click(object sender, EventArgs e)
        {
            this.PageCurrent += 1;
            if (PageCurrent >= PageCount)
            {
                PageCurrent = PageCount - 1;
            }
            this.Bind();
        }

        /// <summary>
        /// 最后页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLast_Click(object sender, EventArgs e)
        {
            PageCurrent = PageCount - 1;
            this.Bind();
        }

        /// <summary>
        /// 点击了刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void btnGo_Click(object sender, EventArgs e)
        {
            try
            {
                if (Int32.TryParse(cmbPagecount.SelectedItem.ToString(), out _pageCurrent))
                {
                    this.Bind();
                }
            }
            catch
            {

            }
        }

        /// <summary>
        /// 提交了转到第几页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbPagecount_DropDownClosed(object sender, EventArgs e)
        {
            if (null != cmbPagecount.SelectedItem)
            {
                int tmp = 0;
                if (Int32.TryParse(cmbPagecount.SelectedItem.ToString(), out tmp))
                {
                    _pageCurrent = tmp - 1;
                    this.Bind();
                }
            }
        }

        /// <summary>
        /// 提交了每页显示的条数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripComboBox1_DropDownClosed(object sender, EventArgs e)
        {
            if (Int32.TryParse(cmbPageSize.SelectedItem.ToString(), out _pageSize))
            {
                this.Bind();
            }  
        }

        private void toolBtnExcel_Click(object sender, EventArgs e)
        {
            if (this.EventExcel != null)
            {
                if (DialogResult.OK == MessageBoxEx.Show(null, "确定导出Excel吗？", "提示", Enums.MessageBoxIconEx.Question, Enums.MessageBoxButtonsEx.OKCancel))
                {
                    this.Epa = this.EventExcel();
                    Tools.FormHelper.ExportExcel(string.Format("{0}_{1}", DateTime.Now.ToString("yyyy-MM-dd"), this.Epa.Title), this.Epa.DataView);
                }
            }
            else
            {
                MessageBoxEx.Show(null, "不具备Excel导出功能", "提示", Enums.MessageBoxIconEx.Information, Enums.MessageBoxButtonsEx.OK);
            }
        }

        private void toolBtnDel_Click(object sender, EventArgs e)
        {
            if (null == this.EventDelItem)
            {
                MessageBoxEx.Show(null, "不具备删除功能", "提示", Enums.MessageBoxIconEx.Information, Enums.MessageBoxButtonsEx.OK);
            }
            else
            {
                if (DialogResult.OK == MessageBoxEx.Show(null, "确定删除吗？", "提示", Enums.MessageBoxIconEx.Question, Enums.MessageBoxButtonsEx.OKCancel))
                {
                    this.Epa = this.EventDelItem();

                    List<Object> objects = ((IEnumerable)this.Epa.DataView.DataSource).OfType<object>().ToList();
                    Type sourceType = objects[0].GetType();
                    List<string> list = new List<string>();
                    for (int i = 0; i < this.Epa.DataView.RowCount; i++)
                    {
                        if (!(this.Epa.DataView.Rows[i].Cells[0] is DataGridViewCheckBoxCellEx))
                        {
                            MessageBox.Show("没有删除功能！");
                            return;
                        }

                        DataGridViewCheckBoxCellEx cell =
                            this.Epa.DataView.Rows[i].Cells[0] as DataGridViewCheckBoxCellEx;

                        if (cell.Checked)
                        {
                            list.Add(this.Epa.DataView.Rows[i].Cells[1].Value.ToString());
                        }
                    }

                    if (list.Count <= 0)
                    {
                        MessageBox.Show("请勾选要删除的数据！");
                        return;
                    }

                    string typeNameSpace = sourceType.BaseType.Namespace;

                    BaseManagerDao dao = new BaseManagerDao(sourceType);

                    for (int i = 0; i < list.Count; i++)
                    {
                        dao.deleteItem(list[i]);
                    }

                    Bind();
                }
            }
        }
    }


    /// <summary>
    /// 自定义事件数据基类
    /// </summary>
    public class EventPagingArg : EventArgs
    {
        private DataGridView _dataView;

        public DataGridView DataView
        {
            get { return _dataView; }
            set { _dataView = value; }
        }

        private string _title;

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        public EventPagingArg(string title, DataGridView dataView)
        {
            _title = title;
            _dataView = dataView;
        }
    }
}
